iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 24
0

Leecode 239. Sliding Window Maximum

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.

作法

def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
    if len(nums) == 0: return []
    max_value = max(nums[:k])
    res = [max_value]
    for i in range(len(nums)-k):
        if nums[i+k] > max_value:
            max_value = nums[i+k]
        elif nums[i] == max_value:
            max_value = max(nums[i+1:i+k+1])
            res.append(max_value)
    return res

上一篇
[Day27] 可以動的規劃?
下一篇
[Day29] 實例演練:質數表
系列文
從0開始學習程式-Python32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言